<template>
<div  style="height: calc( 100% - 87px );">
  <el-tree
    ref="tree"
    show-checkbox
    :data="treeData" 
    node-key="m_id"
    :default-checked-keys="checkedArr"
    :props="defaultProps"
    default-expand-all
    check-strictly
    @check="handleCheck"
  >
  </el-tree>
  <!-- 分页组件 -->
  <PaginationRight
    :isShow="false"
    @sendPushButton="pushButton"
  >
  </PaginationRight>
</div>
</template>

<script>
export default {
  components: {
  },
  data(){
    return{
      defaultProps: {
        label: 'm_name',
        children: 'son'
      },
      treeData: [],
      role_id:'',
      checkedArr:[],//当前选中菜单id数组
      pageinfo: {
        number_all: 0,
      },
    }
  },
  computed: {
      tabsId() {
          return this.$store.state.tabsId;
      }
  },
  mounted(){
    eventBus.$on('sendDataRoleAuthorization',data=>{
      this.role_id=data
      this.getCheckedTreeList()
      this.getTreeList()
    })
  },
  methods:{
    //选中菜单对进行增删
    handleCheck(data,obj){
      console.log(data,obj)
      let temp=obj.checkedKeys.findIndex(item=>{return item==data.m_id})==-1
      if(!temp){
        this.getParentNode(this.$refs.tree.getNode(data.m_id))
        this.getTrueChildNode(this.$refs.tree.getNode(data.m_id))
      }else{
        this.getFalseChildNode(this.$refs.tree.getNode(data.m_id))
        console.log(this.checkedArr)
        this.$refs.tree.setCheckedKeys(this.checkedArr)
      }
    },
    getParentNode(node){
        if(!!node.parent){
          this.checkedArr.push(node.data.m_id)
          this.checkedArr= Array.from(new Set(this.checkedArr))
          this.getParentNode(node.parent)
        }else{
          return 
        }
      },
    getTrueChildNode(node){
      if(!!node.childNodes){
        node.childNodes.forEach(item=>{
          this.checkedArr.push(item.data.m_id)
          this.checkedArr= Array.from(new Set(this.checkedArr))
          this.getTrueChildNode(item)
        })
      }else{
          return 
      }
    },
    getFalseChildNode(node){
      this.checkedArr=this.checkedArr.filter(item=>{return item!=node.data.m_id})
      this.checkedArr= Array.from(new Set(this.checkedArr))
      if(!!node.childNodes){
        node.childNodes.forEach(item=>{
          this.getFalseChildNode(item)
        })
      }else{
          return 
      }
    },
    getSonId(arr){
        arr.forEach(item => {
          this.checkedArr.push(item.m_id)
          if(!!item.hasSon){
            this.checkedArr= Array.from(new Set(this.checkedArr))
            this.getSonId(item.son)
          }else{
            return 
          }
        });
      },
    //底部按钮点击事件
    pushButton(route){
      if(this.role_id){
        if(route=='preservation'){
          // console.log(this.checkedArr)
          let arr=this.checkedArr.map((obj)=>{return ""+obj})
          let params = new FormData();
            params.append("role_id", this.role_id);
            params.append("m_ids", JSON.stringify(arr));
          this.$postForm('saveRoleMenu',params).then((res) => {
            if(res.success){
              this.$message.success(res.msg);
              this.$emit("success");
            }
          });
        }else if(route=='true'){
          //全选
          this.getSonId(this.treeData)
        }else if(route=='flase'){
          //取消选中
          this.checkedArr=[]
          this.$refs.tree.setCheckedKeys([]);
          console.log(this.checkedArr)
        }
      }else{
        this.$message.warning(this.$t('AuthorityLang.message'));
      }
    },
    //获取树形数据
    getTreeList(){
      this.$get('menulists').then((res) => {
        this.treeData=res.data
      })
    },
    //获取树形初始选中数据
    getCheckedTreeList(){
      this.$get('getRoleMenuids',{role_id:this.role_id}).then((res) => {
        this.checkedArr=res.data
      })
    },
  }
}
</script>
<style scoped lang="less">
.el-tree{
    overflow: auto;
    height: 100%;
}
</style>